package yr.website

import grails.converters.JSON
import org.grails.datastore.mapping.query.Query
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class JobbController {
    def jobbService
    static final Logger logger = LoggerFactory.getLogger(JobbController.class)

    def add() {
        def id = Integer.parseInt("${params.i}")
        def job = Jobb.findById(id)
        render(view: "/information", model: [data: job.name])
    }

    def getAll() {
        def list = Jobb.list()
        render(view: "/", model: [data: list])
    }

    def getBy() {
        def data = [:]
        def id = Integer.parseInt("${params.id}")
        def job = Jobb.findById(id)
        println id + "id"
        println job.name
        render(view: "/information", model: job.name)

    }

    def get() {
        def data = [:]
        def name = "${params.name}"
        def a = Jobb.createCriteria()
        def list = a.list() {
            and {
                if (!name.isEmpty()) {
                    ilike("name", "%${name}%")
                }
            }
            order("releaseTime", "desc")
        }
        data << [statuCode: "0", data: list]
        render data as JSON
    }

    def save() {
        def data = [:]
        def job = new Jobb()
        def json = request.JSON
        def name = json.name
        def require = json.require
        try {
            def j = Jobb.findByName(name)
            if (j) {
                data << [statuCode: "2"]
            } else {
                def flag = jobbService.add(name, require)
                if (flag) {
                    def list = Jobb.list()
                    new Logg(recordor: session.getAttribute("username"), recordDate: new Date(), type: "发布新的岗位招聘信息").save()
                    data << [statuCode: "0", data: list]
                } else {
                    data << [statuCode: "1"]
                }
            }
            render data as JSON
        } catch (Exception e) {
            logger.error("JobbController save method " + e.message)
        }
    }

    def refresh() {
        def data = [:]
        def json = request.JSON
        def name = json.name
        def id = json.id
        def require = json.require
        try {
            def job = Jobb.findById(id)
            job.name = name
            job.require = require
            job.releaseTime = new Date()
            def flag = jobbService.save(job)
            if (flag) {
                new Logg(recordor: session.getAttribute("username"), recordDate: new Date(), type: "修改 " + name + " 招聘信息").save()
                data << [statuCode: "0", errMsg: "0"]
            } else {
                data << [statuCode: "1", errMsg: "1"]
            }
            render data as JSON
        } catch (Exception e) {
            logger.error("JobbController Exception refresh " + e.message)
        }
    }

    def del() {
        def data = [:]
        def id = Integer.parseInt("${params.id ?: ''}")
        def job = Jobb.findById(id)
        jobbService.del(id)
        def list = Jobb.list(sort: "releaseTime", order: "desc")
        new Logg(recordor: session.getAttribute("username"), recordDate: new Date(), type: "删除 " + job.name + " 招聘信息").save()
        data << [statuCode: "0", errMsg: "0", data: list]
        render data as JSON
    }

    def changeState() {
        def data = [:]
        def id = Integer.parseInt("${params.id}")
        def state = "${params.state ?: ''}"
        println state + "state"
        def job = Jobb.findById(id)
        println job.state
        if (state == "true") {
            job.state = true
        } else {
            job.state = false
        }
        def flag = jobbService.save(job)
        if (flag) {
            new Logg(recordor: session.getAttribute("username"), recordDate: new Date(), type: "更改 " + job.name + "的发布状态").save()
            data << [statuCode: "0", errMsg: "0"]
        } else {
            data << [statuCode: "1", errMsg: "1"]
        }
        render data as JSON
    }
}
